<?php
namespace User\Logic;

use Think\Model;

class ZJDailyCardLogic extends Model
{
    protected $tableName = "zjrb_card";

    public function generate( $condition )
    {
        $placeholder = str_pad( "", strlen( "".$condition['base_on_increment'] ), '_' );
        $max_card = $this->where( array( "cardnumber" => array( "LIKE", $condition['base_on_year'].$placeholder ) ) )->order( 'id desc' )->limit(1)->getField( "cardnumber" );//TODO: base, increment
        $max_card_arr['year'] = substr( $max_card, 0, 4 );
        if( !$max_card ){
            $max_card_arr['number'] = '0';
        }else{
            $max_card_arr['number'] = substr( $max_card, 4 );
        }

        $this->startTrans();
        $data = array( 'customer_id' => -1, 'addr_id' => -1, 'cardnumber' => "", "password" => "", 'begintime' => $condition['begintime'], 'endtime' => $condition['endtime'], "status" => -1, 'distributionyear' => $condition['base_on_year'] );
        $saved = 0;//TODO: LOCK
        for( $i = 1; $i <= intval( $condition['count'] ) && ( intval( $max_card_arr['number'] ) + $i ) <= intval( $condition['base_on_increment'] ); $i++ )//TODO: batch add
        {
            trace( intval( $max_card_arr['number'] ) + $i, "card" );
            //$data['cardnumber'] = $condition['base_on_year'].(intval( $max_card_arr['number'] ) + i);
            $data['cardnumber'] = implode( array( $condition['base_on_year'], sprintf( "%0".strlen( $condition['base_on_increment'] )."d", intval( $max_card_arr['number'] ) + $i ) ) );
            //$data['cardnumber'] = intval( $max_card_arr['number'] ) + $i;
            $data['password'] = $this->password( intval( $condition['use_password_type'] ) );
            $data['distributionyear'] = $condition['base_on_year'];
            $saved = $this->add( $data );
            if( !$saved ){
                $this->rollback();
            }
        }
        if( $saved ){
            $this->commit();
        }

        return $saved;
    }

    public function queryDiffSet( $condition )
    {
        $placeholder = str_pad( "", strlen( "".$condition['base_on_increment'] ), "_" );
        $max_card = $this->where( array( "cardnumber" => array( 'LIKE', $condition['base_on_year'].$placeholder ) ) )->order( 'id desc' )->limit( 1 )->getField( "cardnumber" );
        if( !$max_card ){
            return $condition['base_on_increment'];
        }else{
            $max_card_arr['number'] = substr( $max_card, 4 );
            return intval( $condition['base_on_increment'] ) - intval( $max_card_arr['number'] );
        }
    }

    public function setStatus( $condition, $status )
    {
    }

    private function password( $type )
    {
        $maxRandom = 0;
        $minRandom = 0;

        if( $type == 1 ){
            $maxRandom = 9;
        }else if( $type == 2 ){
            $maxRandom = 61;
        }else if( $type == 3 || $type == 4 ){
            $maxRandom = 35;
        }

        $arr_num = array( '6', '1', '2', '3', '4', '5', '6', '7', '8', '9' );
        $arr_lowcase_alph = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' );
        $arr_upcase_alph = array( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'  );
        $password = array();

        do {
            $i = count( $password );
            $index = rand( $minRandom, $maxRandom );
             if( $index <= 9 ){
                 $password[$i] = $arr_num[$index];
             }else if( $index <= 35 ){
                 $password[$i] = $type == 4 ? strtoupper( $arr_lowcase_alph[ ( $index-10 ) ] ) : $arr_lowcase_alph[ ($index - 10) ];
             }else if( $index <= 61 ){
                 $password[$i] = $arr_upcase_alph[ ( $index-36 ) ];
             }
        }while( count($password) < 6 );

        return implode( $password );
    }

}
?>
